package my.mark.mybaibaoxiang.eventDemo.easyEvent;


/**
 * @author twotiger-wxm.
 * @date 2019-9-11.
 */
public class Door {

    /**==================单例模式begin=================*/
    private final static Door INSTANCE = new Door(new DoorStateListener(),new DoorNameListener());
    //私有化构造函数，不让外部调用。
    private Door(){}
    private Door(DoorStateListener stateListener,DoorNameListener  nameListener){
        this.stateListener= stateListener;
        this.nameListener= nameListener;
    }

    public static Door getInstance(){
        return INSTANCE;
    }
    /**==================单例模式end=================*/

    private String state = "";
    private String name = "";
    private DoorStateListener stateListener;
    private DoorNameListener  nameListener;

    public void setState(String newValue) {
        if (state != newValue) {
            state = newValue;
            if (stateListener != null){
                //注意参数传递
                DoorEvent event = new DoorEvent(this, "state",newValue);
                stateListener.doorEvent(event);
            }
        }
    }

    public void setName(String newValue) {
        if (name != newValue) {
            name = newValue;
            if (nameListener != null){
                DoorEvent event = new DoorEvent(this,"name", newValue);
                nameListener.doorEvent(event);
            }
        }
    }

    public void setStateListener(DoorStateListener stateListener) {
        this.stateListener = stateListener;
    }

    public void setNameListener(DoorNameListener nameListener) {
        this.nameListener = nameListener;
    }

    public String getState() {
        return state;
    }

    public String getName() {
        return name;
    }

}
